<!DOCTYPE HTML>
<html lang="en">
<head>
<title>GuiControl - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The GuiControl command makes a variety of changes to a control in a GUI window." />
<meta name="ahk:equiv-v2" content="objects/GuiControl.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>GuiControl</h1>

<p>Makes a variety of changes to a control in a GUI window.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, <a href="#SubCommands">SubCommand</a>, ControlID <span class="optional">, Value</span></pre>

<h2>Parameters</h2>
<dl>
  <dt>SubCommand, Value</dt>
  <dd>These are dependent upon each other and their usage is described <a href="#SubCommands">below</a>.</dd>

  <dt>ControlID</dt>
  <dd><p>If the target control has an associated variable, specify the variable's name as the <em>ControlID</em> (this method takes precedence over the ones described next). For this reason, it is usually best to assign a variable to any control that will later be accessed via GuiControl or GuiControlGet, even if that control is not an input-capable type (such as GroupBox or Text).</p>
  <p>Otherwise, <em>ControlID</em> can be either ClassNN (the classname and instance number of the control) or the control's text, both of which can be determined via Window Spy. When using text, the matching behavior is determined by <a href="SetTitleMatchMode.htm">SetTitleMatchMode</a>.</p>
    <p class="note"><strong>Note</strong>: A picture control's file name (as it was specified at the time the control was created) may be used as its <em>ControlID</em>.</p>
  <p><span class="ver">[v1.1.04+]:</span> <em>ControlID</em> can be the <a href="Gui.htm#HwndOutputVar">HWND</a> of a control.</p>
  <p>If the control is not on the default GUI, <strong>the name or HWND of the GUI must also be specified</strong> -- except on <span class="ver">[v1.1.20+]</span> when <em>ControlID</em> is a HWND, since each HWND is unique. See <a href="#Remarks">Remarks</a> for details.</p>
  </dd>
</dl>

<h2 id="SubCommands">Sub-commands</h2>
<p>For <em>SubCommand</em>, specify one of the following:</p>
<ul>
  <li><a href="#Blank">(Blank)</a>: Puts new contents into the control.</li>
  <li><a href="#Text">Text</a>: Changes the text/caption of the control.</li>
  <li><a href="#Move">Move</a>: Moves and/or resizes the control.</li>
  <li><a href="#MoveDraw">MoveDraw</a>: Moves and/or resizes the control and repaints the region occupied by it.</li>
  <li><a href="#Focus">Focus</a>: Sets keyboard focus to the control.</li>
  <li><a href="#Disable">Disable</a>: Disables (grays out) the control.</li>
  <li><a href="#Enable">Enable</a>: Enables the control.</li>
  <li><a href="#Hide">Hide</a>: Hides the control.</li>
  <li><a href="#Show">Show</a>: Shows the control.</li>
  <li><a href="#Delete">Delete</a>: Not yet implemented.</li>
  <li><a href="#Choose">Choose</a>: Selects the specified item number in a multi-item control.</li>
  <li><a href="#ChooseString">ChooseString</a>: Selects a item in a multi-item control whose leading part matches a string.</li>
  <li><a href="#Font">Font</a>: Changes the control's font typeface, size, color, and style.</li>
  <li><a href="#options">Options</a>: Add or remove various control-specific or general options and styles.</li>
</ul>

<h3 id="Blank">(Blank)</h3>
<p>Puts new contents into the control.</p>
<pre class="Syntax"><span class="func">GuiControl</span>,, ControlID <span class="optional">, Value</span></pre>
<p>Leave <em>SubCommand</em> blank to put new contents into the control via <em>Value</em>. Specifically:</p>
<p><a href="GuiControls.htm#Picture">Picture</a>: <em>Value</em> should be the filename (or <a href="../misc/ImageHandles.htm">handle</a>) of the new image to load (see <a href="GuiControls.htm#Picture">Gui Picture</a> for supported file types). Zero or more of the following options may be specified immediately in front of the filename: <code>*wN</code> (width N), <code>*hN</code> (height N), and <code>*IconN</code> (icon group number N in a DLL or EXE file). In the following example, the default icon from the second icon group is loaded with a width of 100 and an automatic height via &quot;keep aspect ratio&quot;: <code>GuiControl,, MyPic, *icon2 *w100 *h-1 C:\My Application.exe</code>. Specify <code>*w0 *h0</code> to use the image's actual width and height. If <code>*w</code> and <code>*h</code> are omitted, the image will be scaled to fit the current size of the control. When loading from a multi-icon .ICO file, specifying a width and height also determines which icon to load.</p>
<p class="note"><strong>Note</strong>: Use only one space or tab between the final option and the filename itself; any other spaces and tabs are treated as part of the filename.</p>
<p><a href="GuiControls.htm#Text">Text</a>/<a href="GuiControls.htm#Button">Button</a>/<a href="GuiControls.htm#GroupBox">GroupBox</a>/<a href="GuiControls.htm#StatusBar">StatusBar</a>/<a href="GuiControls.htm#Link">Link</a>: Specify for <em>Value</em> the control's new text. Since the control will not expand automatically, use <code><a href="#Move">GuiControl, Move</a>, MyText, W300</code> if the control needs to be widened. For <a href="GuiControls.htm#StatusBar">StatusBar</a>, this sets the text of the first part only (use <a href="GuiControls.htm#SB_SetText">SB_SetText()</a> for greater flexibility).</p>
<p><a href="GuiControls.htm#Edit">Edit</a>: Any linefeeds (`n) in <em>Value</em> that lack a preceding carriage return (`r) are automatically translated to CR+LF (`r`n) to make them display properly. However, this is usually not a concern because the <code>Gui Submit</code> and <code>GuiControlGet OutputVar</code> commands will automatically undo this translation by replacing CR+LF with LF (`n).</p>
<p><a href="GuiControls.htm#Hotkey">Hotkey</a>: <em>Value</em> can be blank to clear the control, or a set of modifiers with a key name. Examples: <code>^!c</code>, <code>^Numpad1</code>, <code>+Home</code>. The only modifiers supported are ^ (<kbd>Control</kbd>), ! (<kbd>Alt</kbd>), and + (<kbd>Shift</kbd>). See the <a href="../KeyList.htm">key list</a> for available key names.</p>
<p><a href="GuiControls.htm#Checkbox">Checkbox</a>: <em>Value</em> can be 0 to uncheck the button, 1 to check it, or -1 to give it a gray checkmark. Otherwise, <em>Value</em> is assumed to be the control's new caption/text. See the <a href="#Text">Text</a> sub-command below for how to override this behavior.</p>
<p><a href="GuiControls.htm#Radio">Radio</a>: Same as Checkbox above. However, if the radio button is being checked (turned on) and it is a member of a multi-radio group, the other radio buttons in its group will be automatically unchecked. To check a new button within a radio group that only has one variable, specify for <em>ControlID</em> the name/text of the button if it is not the button with which the variable is directly associated.</p>
<p><a href="GuiControls.htm#DateTime">DateTime</a>/<a href="GuiControls.htm#MonthCal">MonthCal</a>: Specify for <em>Value</em> a date-time stamp in <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a> format. Specify <code>%A_Now%</code> to use the current date and time (today). For DateTime controls, <em>Value</em> may be omitted to cause the control to have no date/time selected (if it was created with <a href="GuiControls.htm#ChooseNone">that ability</a>). For MonthCal controls, a range may be specified if the control is <a href="GuiControls.htm#MonthCalMulti">multi-select</a>.</p>
<p><a href="GuiControls.htm#UpDown">UpDown</a>/<a href="GuiControls.htm#Slider">Slider</a>/<a href="GuiControls.htm#Progress">Progress</a>: <em>Value</em> should be the new position of the control. If a <em>Value</em>'s first character is a plus sign, the number will be assumed to be an offset from the current position. For example, <code>+10</code> would increase the position by 10 and <code>+-10</code> (plus minus ten) would decrease it by 10. If the new position would be outside the range of the control, the control is generally set to the nearest valid value.</p>
<p><a href="GuiControls.htm#Tab">Tab</a>/<a href="GuiControls.htm#DropDownList">DropDownList</a>/<a href="GuiControls.htm#ComboBox">ComboBox</a>/<a href="GuiControls.htm#ListBox">ListBox</a>: <em>Value</em> should contain a pipe-delimited list of entries to be appended at the end of the control's list. To replace (overwrite) the list instead, include a pipe as the first character (e.g. <code>|Red|Green|Blue</code>). To make the control empty, specify only a pipe character (|). To have one of the entries pre-selected, include two pipes after it (e.g. <code>Red|Green||Blue</code>). The separator between fields may be changed to something other than pipe. For example, <code><a href="Gui.htm#Delimiter">Gui +Delimiter`n</a></code> would change it to linefeed and <code>Gui +DelimiterTab</code> would change it to tab (`t).</p>
<p><a href="GuiControls.htm#Tab">Tab controls</a>: In addition to the behavior described in the paragraph above, a tab's sub-controls stay associated with their original tab number; that is, they are never associated with their tab's actual display-name. For this reason, renaming or removing a tab will not change the tab number to which the sub-controls belong. For example, if there are three tabs &quot;Red|Green|Blue&quot; and the second tab is removed by means of <code>GuiControl,, MyTab, |Red|Blue</code>, the sub-controls originally associated with Green will now be associated with Blue. Because of this behavior, only tabs at the end should generally be removed. Tabs that are removed in this way can be added back later, at which time they will reclaim their original set of controls.</p>
<p><a href="ListView.htm">ListView</a> and <a href="TreeView.htm">TreeView</a>: These are not supported when <em>SubCommand</em> is blank. Instead, use the built-in <a href="ListView.htm#BuiltIn">ListView functions</a> and <a href="TreeView.htm#BuiltIn">TreeView functions</a>.</p>

<h3 id="Text">Text</h3>
<p>Changes the text/caption of the control.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, Text, ControlID <span class="optional">, Value</span></pre>
<p>Behaves the same as the <a href="#Blank">blank</a> sub-command above except for:</p>
<p><a href="GuiControls.htm#Checkbox">Checkbox</a>/<a href="GuiControls.htm#Radio">Radio</a>: <em>Value</em> is treated as the new text/caption even if it is -1, 0, or 1.</p>
<p><a href="GuiControls.htm#DateTime">DateTime</a>: <em>Value</em> is treated as the new <a href="GuiControls.htm#DateTimeFormat">date/time format</a> displayed by the control. If <em>Value</em> is omitted, any custom format is removed and the short-date format is put into effect.</p>
<p><a href="GuiControls.htm#ComboBox">ComboBox</a>: <em>Value</em> is treated as the text to put directly into the ComboBox's Edit control.</p>

<h3 id="Move">Move</h3>
<p>Moves and/or resizes the control.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, Move, ControlID, Options</pre>
<p>Specify one or more of the following option letters in <em>Options</em>: X (the x-coordinate relative to the GUI window's client area, which is the area not including title bar, menu bar, and borders); Y (the y-coordinate), W (Width), H (Height). (Specify each number as decimal, not hexadecimal.) For example:</p>
<pre>GuiControl, Move, MyEdit, x10 y20 w200 h100
GuiControl, Move, MyEdit, % &quot;x&quot; VarX+10 &quot;y&quot; VarY+5 &quot;w&quot; VarW*2 &quot;h&quot; VarH*1.5 <em>; Uses an <a href="../Variables.htm#Expressions">expression</a> via &quot;% &quot; prefix.</em></pre>

<h3 id="MoveDraw">MoveDraw</h3>
<p>Moves and/or resizes the control and repaints the region of the GUI window occupied by the control.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, MoveDraw, ControlID <span class="optional">, Options</span></pre>
<p>See the <a href="#Move">Move</a> sub-command (above) for details. Although this may cause an unwanted flickering effect when called repeatedly and rapidly, it solves painting artifacts for certain control types such as <a href="GuiControls.htm#GroupBox">GroupBoxes</a>. <span class="ver">[v1.0.48.04+]</span>: The last parameter may be omitted to redraw the control without moving or resizing it.</p>

<h3 id="Focus">Focus</h3>
<p>Sets keyboard focus to the control.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, Focus, ControlID</pre>
<p>To be effective, the window generally must not be minimized or hidden.</p>

<div id="EnableDisable">
  <h3 id="Disable">Disable</h3>
  <p>Disables (grays out) the control.</p>
  <pre class="Syntax"><span class="func">GuiControl</span>, Disable, ControlID</pre>
  <p>For Tab controls, this will also disable all of the tab's sub-controls. The word Disable may optionally be followed immediately by a 0 or 1. A zero causes the effect to be inverted. For example, <code>Disable</code> and <code>Disable%VarContainingOne%</code> would both disable the control, but <code>Disable%VarContainingZero%</code> would enable it.</p>

  <h3 id="Enable">Enable</h3>
  <p>Enables the control.</p>
  <pre class="Syntax"><span class="func">GuiControl</span>, Enable, ControlID</pre>
  <p>For Tab controls, this will also enable all of the tab's sub-controls. However, any sub-control explicitly disabled via the <a href="#Disable">Disable</a> sub-command (above) will remember that setting and thus remain disabled even after its Tab control is re-enabled. The word Enable may optionally be followed immediately by a 0 or 1. A zero causes the effect to be inverted. For example, <code>Enable</code> and <code>Enable%VarContainingOne%</code> would both enable the control, but <code>Enable%VarContainingZero%</code> would disable it.</p>
</div>

<h3 id="Hide">Hide</h3>
<p>Hides the control.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, Hide, ControlID</pre>
<p>For Tab controls, this will also hide all of the tab's sub-controls. If you additionally want to prevent a control's shortcut key (underlined letter) from working, disable the control via the <a href="#Disable">Disable</a> sub-command. The word Hide may optionally be followed immediately by a 0 or 1. A zero causes the effect to be inverted. For example, <code>Hide</code> and <code>Hide%VarContainingOne%</code> would both hide the control, but <code>Hide%VarContainingZero%</code> would show it.</p>

<h3 id="Show">Show</h3>
<p>Shows the control.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, Show, ControlID</pre>
<p>For Tab controls, this will also show all of the tab's sub-controls. The word Show may optionally be followed immediately by a 0 or 1. A zero causes the effect to be inverted. For example, <code>Show</code> and <code>Show%VarContainingOne%</code> would both show the control, but <code>Show%VarContainingZero%</code> would hide it.</p>

<h3 id="Delete">Delete</h3>
<p><strong>Not yet implemented</strong>: This sub-command does not yet exist. As a workaround, use the sub-commands <a href="#Hide">Hide</a> and/or <a href="#Disable">Disable</a> (above), or destroy and recreate the entire window via <a href="Gui.htm#Destroy">Gui Destroy</a>.</p>

<h3 id="Choose">Choose</h3>
<p>Sets the selection in a ListBox, DropDownList, ComboBox, or Tab control to be the Nth entry.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, Choose, ControlID, N</pre>
<p><em>N</em> should be 1 for the first entry, 2 for the second, etc. If <em>N</em> is not an integer, the <a href="#ChooseString">ChooseString</a> sub-command (below) will be used instead. <span class="ver">[v1.1.06+]:</span> If <em>N</em> is zero, the ListBox, DropDownList or ComboBox's current selection is removed.</p>
<p>Unlike <a href="Control.htm">Control Choose</a>, this sub-command will not trigger any <a href="Gui.htm#label">g-label</a> associated with the control unless <em>N</em> is preceded by a pipe character (even then, the g-label is triggered only when the new selection is different than the old one, at least for <a href="GuiControls.htm#Tab">Tab controls</a>). For example: <code>GuiControl, Choose, MyListBox, <strong>|3</strong></code>.</p>
<p>To additionally cause a finishing event to occur (a double-click in the case of ListBox), include two leading pipes instead of one (this is not supported for Tab controls).</p>
<p>To select or deselect <strong>all</strong> items in a <a href="GuiControls.htm#ListBoxMulti">multi-select ListBox</a>, follow this example:</p>
<pre>Gui +LastFound  <em>; Avoids the need to specify WinTitle below.</em>
<a href="PostMessage.htm">PostMessage</a>, 0x185, 1, -1, ListBox1  <em>; Select all items. 0x185 is LB_SETSEL.</em>
<a href="PostMessage.htm">PostMessage</a>, 0x185, 0, -1, ListBox1  <em>; Deselect all items.</em>
GuiControl, Choose, ListBox1, 0  <em>; Deselect all items <span class="ver">[requires v1.1.06+]</span>.</em></pre>

<h3 id="ChooseString">ChooseString</h3>
<p>Sets the selection (choice) in a ListBox, DropDownList, ComboBox, or Tab control to be the entry whose leading part matches <em>String</em>.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, ChooseString, ControlID, String</pre>
<p>The search is not case sensitive. For example, if a control contains the item &quot;UNIX Text&quot;, specifying <code>GuiControl, ChooseString, <i>ControlID</i>, unix</code> would be enough to select it. The pipe and double-pipe prefix are also supported (see the <a href="#Choose">Choose</a> sub-command above for details).</p>

<h3 id="Font">Font</h3>
<p>Changes the control's font to the typeface, size, color, and style currently in effect for its window.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, Font, ControlID, Options</pre>
<p>For example:</p>
<pre>Gui, Font, s18 cRed Bold, Verdana  <em>; If desired, use a line like this to set a <a href="Gui.htm#Font">new default font</a> for the window.</em>
GuiControl, Font, MyEdit  <em>; Put the above font into effect for a control.</em></pre>

<h3 id="options">Options</h3>
<p>Add or remove various <a href="GuiControls.htm">control-specific</a> or <a href="Gui.htm#OtherOptions">general</a> options and styles.</p>
<pre class="Syntax"><span class="func">GuiControl</span>, +/-Option1 +/-Option2 ..., ControlID <span class="optional">, Value</span></pre>
<p>In the following example, the <a href="Gui.htm#AltSubmit">AltSubmit</a> option is enabled but control's <a href="Gui.htm#label">g-label</a> is removed:</p>
<pre>GuiControl, +AltSubmit -g, MyListBox</pre>
<p>In the next example, the OK button is made the new default button:</p>
<pre>GuiControl, +Default, OK</pre>
<p>Although <a href="../misc/Styles.htm">styles</a> and extended styles are also recognized, some of them cannot be applied or removed after a control has been created. ErrorLevel is set to 0 if at least one of the specified changes was successfully applied. Otherwise, it is set to 1 to indicate that none of the changes could be applied. Even if a change is successfully applied, the control might choose to ignore it.</p>
<p id="Functor"><span class="ver">[v1.1.20+]:</span> To set a <a href="../objects/Functor.htm">function object</a> for handling <a href="Gui.htm#label">the control's events</a>, <em>Value</em> must be a single variable reference, as in either of the following examples. Other expressions which return objects are currently unsupported.</p>
<pre>GuiControl +g, <i>ControlID</i>, %FuncObj%
GuiControl +g, <i>ControlID</i>, % FuncObj</pre>

<h2>Error Handling</h2>
<p><span class="ver">[v1.1.04+]</span>: This command is able to throw an exception on failure. For more information, see <a href="Catch.htm#RuntimeErrors">Runtime Errors</a>.</p>
<p><a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to 1 if the specified window/control does not exist or some other problem prevented the command from working. Otherwise, it is set to 0.</p>

<h2 id="Remarks">Remarks</h2>
<p>To operate upon a window other than the default, include its <a href="Gui.htm#MultiWin">name or number</a> (or <span class="ver">[in v1.1.03+]</span> its HWND) followed by a colon in front of the sub-command as in these examples:</p>
<pre>GuiControl, MyGui:Show, MyButton
GuiControl, MyGui:, MyListBox, Item1|Item2</pre>
<p>This is required even if <em>ControlID</em> is a control's associated variable, since any one variable can be used on multiple GUI windows. In <span class="ver">[v1.1.20+]</span>, the GUI name can be omitted if <em>ControlID</em> is a control's HWND.</p>
<p>A GUI <a href="../misc/Threads.htm">thread</a> is defined as any thread launched as a result of a GUI action. GUI actions include selecting an item from a GUI window's menu bar, or triggering one of its <a href="Gui.htm#label">g-labels</a> (such as by pressing a button).</p>
<p>The <a href="Gui.htm#DefaultWin">default window name</a> for a GUI thread is that of the window that launched the thread. Non-GUI threads use 1 as their default.</p>

<h2>Related</h2>
<p><a href="Gui.htm">Gui</a>, <a href="GuiControlGet.htm">GuiControlGet</a>, <a href="Control.htm">Control</a></p>

<h2>Examples</h2>

<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>GuiControl,, MyListBox, |Red|Green|Blue  <em>; Replace the current list with a new list.</em>
GuiControl,, MyEdit, New text line 1.`nNew text line 2.
GuiControl,, MyRadio2, 1  <em>; Turn on this radio button and turn off all the others in its group.</em>
GuiControl, Move, OK, x100 y200  <em>; Move the OK button to a new location.</em>
GuiControl, Focus, LastName  <em>; Set keyboard focus to the control whose variable or text is &quot;LastName&quot;.</em></pre>
</div>

</body>
</html>
